/*
 *    Copyright (c) 2018-2025, boss All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *
 * Redistributions of source code must retain the above copyright notice,
 * this list of conditions and the following disclaimer.
 * Redistributions in binary form must reproduce the above copyright
 * notice, this list of conditions and the following disclaimer in the
 * documentation and/or other materials provided with the distribution.
 * Neither the name of the boss developer nor the names of its
 * contributors may be used to endorse or promote products derived from
 * this software without specific prior written permission.
 * Author: boss
 */
package com.hopes.bizz.video.service.service.impl;

import cn.hutool.core.lang.tree.Tree;
import cn.hutool.core.lang.tree.TreeNode;
import cn.hutool.core.lang.tree.TreeUtil;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hopes.bizz.common.core.constant.CommonConstant;
import com.hopes.bizz.video.config.HikSyncConfig;
import com.hopes.bizz.video.service.entity.VideoListEntity;
import com.hopes.bizz.video.service.entity.VideoRegionEntity;
import com.hopes.bizz.video.service.mapper.VideoListMapper;
import com.hopes.bizz.video.service.service.VideoListService;
import com.hopes.bizz.video.service.service.VideoRegionService;
import com.hopes.boss.common.core.util.StringUtils;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;
import java.util.stream.Collectors;

/**
 * 视频
 *
 * @author daijie
 * @date 2023-10-24 13:58:52
 */
@Service
@RequiredArgsConstructor
public class VideoListServiceImpl extends ServiceImpl<VideoListMapper, VideoListEntity> implements VideoListService {

    private final HikSyncConfig hikSyncConfig;
    private final VideoRegionService videoRegionService;

    @Override
    public Boolean sync() {
        // 查询海康视频信息
        String regionIndex = null;
//        if(coVideo.getCompanyId() != null){
//            CoCompany coCompany = coCompanyMapper.getById(coVideo.getCompanyId());
//            if(coCompany.getIscIndex() != null && !coCompany.getIscIndex().equals("")){
//                regionIndex = coCompany.getIscIndex();
//            }
//        }
        JSONArray resourceArray = hikSyncConfig.listHikResource(regionIndex, "camera");

        // 查询视频表中数据
        Set<String> videoIndexSet = new HashSet<>();
        List<VideoListEntity> videoList = this.list();
        for (VideoListEntity video : videoList) {
            videoIndexSet.add(video.getVideoCode());
        }

        // 查询企业编码
//        JSONObject companyIscIndexO = new JSONObject();
//        List<CoCompany> companyList = coCompanyMapper.queryForList(new QueryParams<>());
//        for(int i = 0; i < companyList.size(); i++){
//            CoCompany company = companyList.get(i);
//            if(company.getIscIndex() != null && !company.getIscIndex().equals("")){
//                companyIscIndexO.put(company.getIscIndex(), company.getId());
//            }
//        }

        for(int i = 0; i < resourceArray.size(); i++){
            JSONObject resource = resourceArray.getJSONObject(i);
            String videoIndex = resource.getString("indexCode");
            String regionIndexCode = resource.getString("regionIndexCode");
            if(!videoIndexSet.contains(videoIndex)){
                VideoListEntity video = new VideoListEntity();
                video.setName(resource.getString("name"));
                video.setVideoCode(resource.getString("indexCode"));
                video.setRegionIndex(resource.getString("regionIndexCode"));
                video.setUseFlag(CommonConstant.boolean_true_string);
//                if(companyIscIndexO.containsKey(regionIndexCode)){
//                    video.setCompanyId(companyIscIndexO.getLong(regionIndexCode));
//                }
                this.save(video);
            }
        }
        return true;
    }

    @Override
    public List<Tree<Long>> videoTree(VideoListEntity videoList) {
        List<VideoRegionEntity> regionList = videoRegionService.list();
        List<TreeNode<Long>> tree = regionList.stream().map(region -> {
            TreeNode<Long> treeNode = new TreeNode<>();
            treeNode.setId(region.getId());
            treeNode.setParentId(region.getParentId());
            treeNode.setName(region.getName());
            treeNode.setWeight(region.getSortNum());

            Map<String, Object> extra = new HashMap<>();
            extra.put("videoList", this.list(Wrappers.lambdaQuery(VideoListEntity.class)
                    .eq(VideoListEntity::getRegionIndex, region.getHikIndexCode())
                    .like(StringUtils.isNotBlank(videoList.getName()), VideoListEntity::getName, videoList.getName())));
            treeNode.setExtra(extra);
            return treeNode;
        }).collect(Collectors.toList());

        return TreeUtil.build(tree, 0L);
    }
}
